﻿using Cdx.Ui.App_Start;
using Cdx.Ui.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cdx.Ui.Commands
{
    public class CommandCreateUser: CommandBase
    {
        private bool m_SafeCreate;

        private string m_UserName;
        
        private string m_Password;

        public CommandCreateUser(Model model, string userName, string password, bool safeCreate): base(model)
        {
            m_UserName = userName;
            m_Password = password;

            m_SafeCreate = safeCreate;
        }
        
        public override void Execute()
        {
            try
            {
                if (m_SafeCreate == true && Model.Engine.UserExists(m_UserName))
                {
                    base.Model.Status.Message = string.Format("User {0} already exists", m_UserName);
                }
                else
                {
                    base.Model.Engine.CreateUser(m_UserName, m_Password);
                    base.Model.Status.Message = string.Format("User {0} is created", m_UserName);
                }   
            }
            catch (Exception e)
            {
                Logger.Log.Error(e);
                base.Model.Status.Message = string.Format("Failed to created {0} user", m_UserName);
            }
        }
    }
}